﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using log4net;

namespace SopCastRecorder
{
	public partial class ScheduleControl : UserControl
	{
		private static ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

		public ScheduleControl()
		{
			InitializeComponent();
			this.Init();
		}

		private void Init()
		{
			this.txtAddress.Items.AddRange(OptionFile.Option1.GetChannels());
		}

		private ScheduleInfo _scheduleInfo1;
		public ScheduleInfo scheduleInfo1
		{
			get
			{
				return this._scheduleInfo1;
			}
			set
			{
				this._scheduleInfo1 = value;
				if (this._scheduleInfo1.Path != null)
				{
					this.btnFile.Text = new FileInfo(this._scheduleInfo1.Path).Name;
				}
				this.txtAddress.Text = this._scheduleInfo1.Channel;
				this.dtpDate.Value = this._scheduleInfo1.Date;
				this.dtpStartTime.Value = this._scheduleInfo1.StartTime;
				this.dtpEndTime.Value = this._scheduleInfo1.EndTime;
				this.chbWeekSun.Checked = this._scheduleInfo1.WeekSun;
				this.chbWeekMon.Checked = this._scheduleInfo1.WeekMon;
				this.chbWeekTue.Checked = this._scheduleInfo1.WeekTue;
				this.chbWeekWed.Checked = this._scheduleInfo1.WeekWed;
				this.chbWeekThu.Checked = this._scheduleInfo1.WeekThu;
				this.chbWeekFri.Checked = this._scheduleInfo1.WeekFri;
				this.chbWeekSat.Checked = this._scheduleInfo1.WeekSat;
				this.chbEnable.Checked = this._scheduleInfo1.Enable;
			}
		}

		private void btnFile_Click(object sender, EventArgs e)
		{
			try
			{
				this.File();
			}
			catch (Exception exception1)
			{
				logger.Error(MethodBase.GetCurrentMethod().Name, exception1);
				Form1.SetErrorStatus();
			}
		}

		private void File()
		{
			var dialog = new SaveFileDialog();
			dialog.OverwritePrompt = false;
			if (!string.IsNullOrEmpty(this.scheduleInfo1.Path))
			{
				var f = new FileInfo(this.scheduleInfo1.Path);
				dialog.InitialDirectory = f.DirectoryName;
				dialog.FileName = f.Name;
			}
			if (dialog.ShowDialog() == DialogResult.OK)
			{
				this.scheduleInfo1.Path = dialog.FileName;
				this.btnFile.Text = new FileInfo(dialog.FileName).Name;
			}
		}

		public void RefreshScheduleInfo()
		{
			this._scheduleInfo1.Channel = this.txtAddress.Text;
			this._scheduleInfo1.Date = DateTime.Parse(this.dtpDate.Text);
			this._scheduleInfo1.StartTime = DateTime.Parse(this.dtpDate.Text + " " + this.dtpStartTime.Text);
			this._scheduleInfo1.EndTime = DateTime.Parse(this.dtpDate.Text + " " + this.dtpEndTime.Text);
			this._scheduleInfo1.WeekSun = this.chbWeekSun.Checked;
			this._scheduleInfo1.WeekMon = this.chbWeekMon.Checked;
			this._scheduleInfo1.WeekTue = this.chbWeekTue.Checked;
			this._scheduleInfo1.WeekWed = this.chbWeekWed.Checked;
			this._scheduleInfo1.WeekThu = this.chbWeekThu.Checked;
			this._scheduleInfo1.WeekFri = this.chbWeekFri.Checked;
			this._scheduleInfo1.WeekSat = this.chbWeekSat.Checked;
			this._scheduleInfo1.Enable = this.chbEnable.Checked;
		}

		public Action<ScheduleInfo> DeleteFunction { get; set; }
		private void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
				this.DeleteFunction.Invoke(this.scheduleInfo1);
			}
			catch (Exception exception1)
			{
				logger.Error(MethodBase.GetCurrentMethod().Name, exception1);
				Form1.SetErrorStatus();
			}
		}
	}
}
